package com.umeox.watch.moto.contact.receiver;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.RemoteException;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.agora.AgoraService;
import com.umeox.watch.moto.apiclient.model.VoipTokenDTO;
import com.umeox.watch.moto.contact.base.BaseApp;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.dataservice.proxy.VideocallProxy;

public class VideoReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        final String action = intent.getAction();
        if (action == null) {
            return;
        }


        Logger.i("Receiver action:" + action);
        final AgoraService agoraService = AgoraService.Companion.getInstance(BaseApp.getInstance().getApplicationContext());
        if(Constants.ACTION_CANCEL_AGORA_CALL.equalsIgnoreCase(action)){
            String channel = intent.getStringExtra("channel");
            if(channel.equals(agoraService.getTargetChannelId())){
                agoraService.resetCallState();
            }
            return;
        }


        final boolean callBusy = agoraService.isCallBusy();
        Logger.i("VideoReceiver: 接收到邀请 isCallBusy " + callBusy);
        if (callBusy) {
            return;
        }
        switch (action) {
            case Constants.ACTION_INCOMING_VOIP_CALL:
                agoraService.setCallType(AgoraService.CALL_TYPE_VOICE);
                break;
            case Constants.ACTION_INCOMING_VIDEO_CALL:
                agoraService.setCallType(AgoraService.CALL_TYPE_VIDEO);
                break;
            case Constants.ACTION_INCOMING_PTT_CALL:
                agoraService.setCallType(AgoraService.CALL_TYPE_PTT);
            default:
                break;

        }
        String channel = intent.getStringExtra("channel");
        String fuid = intent.getStringExtra("fuid");
        //请求token
        try {
            VoipTokenDTO voipTokenDTO = VideocallProxy.fetchBinder().getVoipToken(DataProvider.getHolderId(), fuid, channel);
            if (voipTokenDTO != null) {
                Logger.i("voipTokenDTO:" + voipTokenDTO.toString());
                agoraService.setChannelToken(voipTokenDTO.channelToken);
                agoraService.setChannelUserId(voipTokenDTO.rtcMemberId);
                agoraService.setIncomingCall(true);
                if (agoraService.isLoggedIn()) {
                    agoraService.logout();
                }
                new LoginTask().execute(voipTokenDTO.rtmFromMemberId, voipTokenDTO.rtmToken);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    private static class LoginTask extends AsyncTask<String, Void, Void> {


        @Override
        protected Void doInBackground(String... params) {
            String account = params[0];
            String loginToken = params[1];
            AgoraService.Companion.getInstance(BaseApp.getInstance().getApplicationContext()).login(account,loginToken);
            return null;
        }
    }

}